# app.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
@app.route('/')
def index():
    return '''
    <!DOCTYPE html>
    <html>
    <body>
        <input id="input" placeholder="输入消息">
        <button onclick="send()">发送</button>
        <div id="result"></div>

        <script>
            function send() {
                const msg = document.getElementById('input').value;
                fetch('/api/message', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({message: msg})
                })
                .then(res => res.json())
                .then(data => {
                    document.getElementById('result').textContent = data.response;
                });
            }
        </script>
    </body>
    </html>
    '''
@app.route('/api/message', methods=['POST'])
def message():
    data = request.get_json()
    msg = data.get('message')
    if msg == "520":
        return jsonify({"response": "521"})
    return jsonify({"response": f"收到: {msg}"})
if __name__ == '__main__':
    app.run(debug=True)